package com.sixth.controller;


import com.sixth.annotation.ParameterValidation;
import com.sixth.bean.MarketGoods;
import com.sixth.bean.MarketGoodsProduct;
import com.sixth.bean.bo.AdminGoodsCreateBO;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.AdminGoodsDetailVO;
import com.sixth.bean.vo.CatAndBrandVO;
import com.sixth.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qys
 * @Description
 * @date 2022/9/8 13:06
 */
@RestController
@RequestMapping("admin/goods")
@ParameterValidation
public class AdminGoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     *
     * @author qys
     * @date 2022/9/8 13:24
     * @param basePageInfo
     * @return BaseRespVo
     */

    @GetMapping("list")
    public BaseRespVo adminGoodsList(BasePageInfo basePageInfo, String goodsSn, String goodsId, String name) {
        Integer id = null;

//        basePageInfo.setPage(1);

        if (goodsId != null && !"".equals(goodsId)) {
            try {
                id = Integer.parseInt(goodsId);
            }
            catch (NumberFormatException e) {
                // 有非法参数，直接返回空
                return BaseRespVo.ok(null);
                // e.printStackTrace();
            }
        }
        if (goodsSn != null && !"".equals(goodsSn)) {
            try {
                Integer.parseInt(goodsSn);
            }
            catch (NumberFormatException e) {
                // 有非法参数，直接返回空
                return BaseRespVo.ok(null);
                // e.printStackTrace();
            }
        }

        Object adminGoodsListVO = goodsService.adminGoodsList(basePageInfo, goodsSn, id, name);
        return BaseRespVo.ok(adminGoodsListVO);
    }

    /**
     *
     * @author qys
     * @date 2022/9/8 14:49
     * @return BaseRespVo
     */
    @GetMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        CatAndBrandVO catAndBrandVO = goodsService.catAndBrand();
        return BaseRespVo.ok(catAndBrandVO);
    }

    /**
     *
     * @author qys
     * @date 2022/9/9 11:32
     * @param adminGoodsCreateBO
     * @return BaseRespVo
     */
    // TODO: 402参数值不对,611商品名已存在，0成功

    @PostMapping("create")
    public BaseRespVo adminGoodsCreate(@RequestBody AdminGoodsCreateBO adminGoodsCreateBO) {

        // 判断goods参数合法
        MarketGoods goods = adminGoodsCreateBO.getGoods();
        // 商品名和信息判断
        if (goods.getName() == null || goods.getGoodsSn() == null || "".equals(goods.getGoodsSn()) || "".equals(goods.getName())) {
            return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
        }
        // 商品单位判断
        // if (goods.getUnit() != null || "".equals(goods.getUnit().trim())) {
        //     String[] unitLimit = {"件", "盒", "个"};
        //     boolean flag = false;
        //     for (String s : unitLimit) {
        //         if (goods.getUnit().equals(s)) {
        //             flag = true;
        //         }
        //     }
        //     if (flag) {
        //         return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
        //     }
        // }
        // 商品编号必须为数字
        try {
            Integer.parseInt(goods.getGoodsSn());
        }
        catch (NumberFormatException e) {
            return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
        }
        // 商品规格判断
        // TODO: 需要写异常处理，price和number参数接受会抛异常
        List<MarketGoodsProduct> products = adminGoodsCreateBO.getProducts();
        int code = goodsService.adminGoodsCreate(adminGoodsCreateBO);
        if (code == 611) {
            return BaseRespVo.invalidNoAndMsg("商品名已存在", 611);
        }
        return BaseRespVo.ok();
    }

    /**
     *
     * @author qys
     * @date 2022/9/9 11:31
     * @param id
     * @return BaseRespVo
     */
    @GetMapping("detail")
    public BaseRespVo adminGoodsDetail(Integer id) {
        AdminGoodsDetailVO adminGoodsDetailVO = goodsService.adminGoodsDetail(id);
        return BaseRespVo.ok(adminGoodsDetailVO);
    }

    /**
     *
     * @author qys
     * @date 2022/9/9 11:31
     * @param marketGoods
     * @return BaseRespVo
     */
    @PostMapping("delete")
    public BaseRespVo adminGoodsDelete(@RequestBody MarketGoods marketGoods) {
        int code = goodsService.adminGoodsDelete(marketGoods);
        return BaseRespVo.ok();
    }


    /**
     *
     * @author qys
     * @date 2022/9/9 11:31
     * @param adminGoodsCreateBO
     * @return BaseRespVo
     */
    @PostMapping("update")
    public BaseRespVo adminGoodsUpdate(@RequestBody AdminGoodsCreateBO adminGoodsCreateBO) {
        // 判断goods参数合法
        MarketGoods goods = adminGoodsCreateBO.getGoods();
        // 商品名和信息判断
        if (goods.getName() == null || goods.getGoodsSn() == null || "".equals(goods.getGoodsSn()) || "".equals(goods.getName())) {
            return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
        }
        try {
            Integer.parseInt(goods.getGoodsSn());
        }
        catch (NumberFormatException e) {
            return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
        }
        // 商品规格判断
        // TODO: 需要写异常处理，price和number参数接受会抛异常
        List<MarketGoodsProduct> products = adminGoodsCreateBO.getProducts();
        int code = goodsService.adminGoodsUpdate(adminGoodsCreateBO);
        if (code == 611) {
            return BaseRespVo.invalidNoAndMsg("商品名已存在", 611);
        }
        return BaseRespVo.ok(0);
    }
}
